package com.jhgsys.internal.sypq.controller;

import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.sypq.entity.TaskAttach;
import com.jhgsys.internal.sypq.service.ITaskAttachService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import javax.validation.constraints.NotBlank;
import java.util.Date;

/**
 * 任务所需附体 Controller
 *
 * @author ganjun
 * @date 2024-11-18 10:54:56
 */
@Slf4j
@Validated
@Controller
public class TaskAttachController extends BaseController {

    @Autowired
    private ITaskAttachService taskAttachService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "taskAttach")
    @RequiresPermissions("taskAttach:view")
    public String taskAttachIndex(){
        return jumpPage("sypq/taskAttach/taskAttachList");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "taskAttach/add")
    @RequiresPermissions("taskAttach:add")
    public String taskAttachAdd(){
        return jumpPage("sypq/taskAttach/taskAttachEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "taskAttach/update/{id}")
    @RequiresPermissions("taskAttach:edit")
    public String taskAttachUpdate(Model model, @PathVariable("id") String id){
        model.addAttribute("taskAttach", taskAttachService.findTaskAttachById(id));
        return jumpPage("sypq/taskAttach/taskAttachEdit");
    }

    /**
     * 查询（所有）
     * @author ganjun
     * @date 2024-11-18 10:54:56
     */
    @GetMapping("taskAttach")
    @ResponseBody
    @RequiresPermissions("taskAttach:view")
    public FebsResponse getAllTaskAttachs(TaskAttach taskAttach) {
        try {
            return success(taskAttachService.findTaskAttachList(taskAttach));
        }catch (Exception ex){
            return failure(ex);
        }

    }

    /**
     * 查询（分页）
     * @author ganjun
     * @date 2024-11-18 10:54:56
     */
    @GetMapping("taskAttach/list")
    @ResponseBody
    @RequiresPermissions("taskAttach:view")
    public FebsResponse taskAttachList(QueryRequest request, TaskAttach taskAttach) {
        try {
            Map<String, Object> dataTable = getDataTable(this.taskAttachService.findTaskAttachPage(request, taskAttach));
            return success(dataTable);
        }catch (Exception ex){
            return failure(ex);
        }
    }

    /**
     * 新增 TaskAttach
     * @author ganjun
     * @date 2024-11-18 10:54:56
     */
    @Log("新增TaskAttach")
    @PostMapping("taskAttach")
    @ResponseBody
    @RequiresPermissions("taskAttach:add")
    public FebsResponse addTaskAttach(@Valid TaskAttach taskAttach) throws FebsException {
        try {
            this.taskAttachService.createTaskAttach(taskAttach);
            return success();
        } catch (Exception ex) {
            return failure(ex,"新增失败");
        }
    }

    /**
     * 逻辑删除 TaskAttach
     * @author ganjun
     * @date 2024-11-18 10:54:56
     */
    @Log("逻辑删除TaskAttach")
    @GetMapping("taskAttach/delete/{id}")
    @ResponseBody
    @RequiresPermissions("taskAttach:delete")
    public FebsResponse deleteTaskAttach(@PathVariable("id") String taskAttachIds) throws FebsException {
        try {
            TaskAttach taskAttach = new TaskAttach();
            taskAttach.setDeletedBy(getCurrentUser().getUserName());
            taskAttach.setDeletedTime(new Date());
            taskAttach.setDeletedFlg(FebsConstant.INVALID);
            this.taskAttachService.deleteTaskAttach(taskAttach,taskAttachIds);
            return success();
        } catch (Exception ex) {
            return failure(ex,"删除失败");
        }
    }

    /**
    * 物理删除 TaskAttach
    * @author ganjun
    * @date 2024-11-18 10:54:56
    */
    @Log("物理删除TaskAttach")
    @GetMapping("taskAttach/physicallyDelete/{id}")
    @ResponseBody
    @RequiresPermissions("taskAttach:delete")
    public FebsResponse physicallyDeleteTaskAttach(@PathVariable("id") String taskAttachIds) throws FebsException {
        try {
            this.taskAttachService.physicallyDeleteTaskAttach(taskAttachIds);
            return success();
        } catch (Exception ex) {
            return failure(ex,"删除失败");
        }
    }

    /**
     * 修改 TaskAttach
     * @author ganjun
     * @date 2024-11-18 10:54:56
     */
    @Log("修改TaskAttach")
    @PostMapping("taskAttach/update")
    @ResponseBody
    @RequiresPermissions("taskAttach:edit")
    public FebsResponse updateTaskAttach(TaskAttach taskAttach) throws FebsException {
        try {
            this.taskAttachService.updateTaskAttach(taskAttach);
            return success();
        } catch (Exception ex) {
            return failure(ex,"修改失败");
        }
    }

}
